﻿using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace Windows.Toolbar.Controls.Specialized
{
    public partial class LineStylePicker : SelectionContainer
    {
        public LineStylePicker()
        {
            InitializeComponent();
        }

        protected override void OnSelectedValueChanged()
        {
            if (!Object.Equals(LinesListBox.SelectedItem, SelectedValue))
            {
                DoubleCollection source = SelectedValue as DoubleCollection;
                if (source != null)
                {
                    foreach (DoubleCollection targetCollection in LinesListBox.Items)
                    {
                        if (targetCollection.Count == source.Count)
                        {
                            bool areEquals = true;
                            for (int i = 0; i < targetCollection.Count; i++)
                            {
                                if (targetCollection[i] != source[i])
                                {
                                    areEquals = false;
                                    break;
                                }
                            }

                            if (areEquals)
                            {
                                LinesListBox.SelectedItem = targetCollection;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    LinesListBox.SelectedItem = null;
                }
            }

            base.OnSelectedValueChanged();
        }

        private void LinesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedValue != LinesListBox.SelectedItem)
            {
                SelectedValue = LinesListBox.SelectedItem;

                OnSelectedValueChanged();
            }
        }
    }
}
